
#include "ui_pull_page.h"
#include "lvgl.h"
#include <math.h>
#include <stdlib.h>
static lv_obj_t *lv_obj_top = NULL;
static lv_obj_t *lv_obj_left = NULL;
static lv_obj_t *lv_obj_bot = NULL;
static lv_obj_t *lv_obj_right = NULL; 
#define PULL_PAGE_BG_COLOR lv_color_hex(0xffffff) //lv_color_hex(0x191919)
#define PULL_PAGE_BG_OPA   LV_OPA_100
#define PULL_PAGE_AUTO_PRIMING_PIXEL 30
void *pull_page_get(PULL_DIR_e pull_dir)
{
     lv_obj_t *scr = lv_scr_act();
     
     int width = lv_obj_get_width(scr);
     int height = lv_obj_get_height(scr);

     if(!lv_obj_top)
     {
       lv_obj_top = lv_obj_create(lv_layer_top());
       lv_obj_set_pos(lv_obj_top, 0, height);
       lv_obj_set_size(lv_obj_top, width, height);
       lv_obj_set_style_bg_opa(lv_obj_top, 0, PULL_PAGE_BG_OPA);
       lv_obj_set_style_border_width(lv_obj_top , 0 , 0);
       lv_obj_set_style_pad_left(lv_obj_top, 0, 0);
       lv_obj_set_style_bg_color(lv_obj_top , PULL_PAGE_BG_COLOR, 0);
       lv_obj_add_flag(lv_obj_top, LV_OBJ_FLAG_HIDDEN);
       lv_obj_set_style_radius(lv_obj_top, 0, 0);
       lv_obj_move_foreground(lv_obj_top);

       lv_obj_t *bar = lv_obj_create(lv_obj_top);
       lv_obj_set_style_border_width(bar , 0 , 0);
       lv_obj_set_style_width(bar, 60, 0);
       lv_obj_set_style_height(bar, 4, 0);
       lv_obj_set_style_radius(bar, 2, 0);
       lv_obj_set_style_bg_color(bar , lv_color_hex(0x191919), 0);
       lv_obj_align(bar, LV_ALIGN_TOP_MID, 0, -12);
       lv_obj_set_style_bg_opa(bar, 0, LV_OPA_20);
     }

     if(!lv_obj_bot)
     {
       lv_obj_bot = lv_obj_create(lv_layer_top());
       lv_obj_set_pos(lv_obj_bot, 0, -height);
       lv_obj_set_size(lv_obj_bot, width, height);       
       lv_obj_set_style_bg_opa(lv_obj_bot, 0, PULL_PAGE_BG_OPA);
       lv_obj_set_style_border_width(lv_obj_bot , 0 , 0);
       lv_obj_set_style_pad_left(lv_obj_bot, 0, 0);     
       lv_obj_set_style_bg_color(lv_obj_bot , PULL_PAGE_BG_COLOR, 0);
       lv_obj_add_flag(lv_obj_bot, LV_OBJ_FLAG_HIDDEN);
       lv_obj_set_style_radius(lv_obj_bot, 0, 0);
       lv_obj_move_foreground(lv_obj_bot);
     }

     if(!lv_obj_left)
     {
       lv_obj_left = lv_obj_create(lv_layer_top());
       lv_obj_set_pos(lv_obj_left, width, 0);
       lv_obj_set_size(lv_obj_left, width, height);         
       lv_obj_set_style_bg_opa(lv_obj_left, 0, PULL_PAGE_BG_OPA);
       lv_obj_set_style_border_width(lv_obj_left , 0 , 0);
       lv_obj_set_style_pad_left(lv_obj_left, 0, 0);  
       lv_obj_set_style_bg_color(lv_obj_left , PULL_PAGE_BG_COLOR, 0);     
       lv_obj_add_flag(lv_obj_left, LV_OBJ_FLAG_HIDDEN);  
       lv_obj_set_style_radius(lv_obj_left, 0, 0);
       lv_obj_move_foreground(lv_obj_left);
     }     

     if(!lv_obj_right)
     {
       lv_obj_right = lv_obj_create(lv_layer_top());
       lv_obj_set_pos(lv_obj_right, -width, 0);
       lv_obj_set_size(lv_obj_right, width, height);         
       lv_obj_set_style_bg_opa(lv_obj_right, 0, PULL_PAGE_BG_OPA);
       lv_obj_set_style_border_width(lv_obj_right , 0 , 0);
       lv_obj_set_style_pad_left(lv_obj_right, 0, 0); 
       lv_obj_set_style_bg_color(lv_obj_right , PULL_PAGE_BG_COLOR, 0);   
       lv_obj_add_flag(lv_obj_right, LV_OBJ_FLAG_HIDDEN);     
       lv_obj_set_style_radius(lv_obj_right, 0, 0);  
       lv_obj_move_foreground(lv_obj_right);
     }     

     switch(pull_dir)
     {
        case PULL_DIR_BOTTOM: return (void *)lv_obj_bot; break;
        case PULL_DIR_TOP: return (void *)lv_obj_top; break;
        case PULL_DIR_RIGHT: return (void *)lv_obj_right; break;
        case PULL_DIR_LEFT: return (void *)lv_obj_left; break;
        default: return NULL; break;
     }
}

static lv_obj_t *get_cur_active_page(PULL_DIR_e pull_dir)
{
      if(!lv_obj_has_flag(lv_obj_bot, LV_OBJ_FLAG_HIDDEN) && (pull_dir == PULL_DIR_BOTTOM || pull_dir == PULL_DIR_TOP))
      {
         return lv_obj_bot;
      }
      else if(!lv_obj_has_flag(lv_obj_top, LV_OBJ_FLAG_HIDDEN) && (pull_dir == PULL_DIR_BOTTOM || pull_dir == PULL_DIR_TOP))
      {
         return lv_obj_top;
      }
      else if(!lv_obj_has_flag(lv_obj_right, LV_OBJ_FLAG_HIDDEN) && (pull_dir == PULL_DIR_RIGHT || pull_dir == PULL_DIR_LEFT))
      {
         return lv_obj_right;
      }
      else if(!lv_obj_has_flag(lv_obj_left, LV_OBJ_FLAG_HIDDEN) && (pull_dir == PULL_DIR_RIGHT || pull_dir == PULL_DIR_LEFT))
      {
         return lv_obj_left;
      } 

      lv_obj_add_flag(lv_obj_bot, LV_OBJ_FLAG_HIDDEN);  
      lv_obj_add_flag(lv_obj_top, LV_OBJ_FLAG_HIDDEN);  
      lv_obj_add_flag(lv_obj_right, LV_OBJ_FLAG_HIDDEN);  
      lv_obj_add_flag(lv_obj_left, LV_OBJ_FLAG_HIDDEN);  

     switch(pull_dir)
     {
        case PULL_DIR_BOTTOM: 
            lv_obj_clear_flag(lv_obj_bot, LV_OBJ_FLAG_HIDDEN);  
            lv_obj_move_foreground(lv_obj_bot);
            return lv_obj_bot; 
        break;
        case PULL_DIR_TOP: 
            lv_obj_clear_flag(lv_obj_top, LV_OBJ_FLAG_HIDDEN);  
            lv_obj_move_foreground(lv_obj_top);
            return lv_obj_top;  
        break;
        case PULL_DIR_RIGHT: 
            lv_obj_clear_flag(lv_obj_right, LV_OBJ_FLAG_HIDDEN);  
            lv_obj_move_foreground(lv_obj_right);
            return lv_obj_right;       
        break;
        case PULL_DIR_LEFT: 
            lv_obj_clear_flag(lv_obj_left, LV_OBJ_FLAG_HIDDEN);  
            lv_obj_move_foreground(lv_obj_left);
            return lv_obj_left;
        break;
        default:  break;
     }     

     return NULL;           
}

void  pull_page_action(PULL_DIR_e pull_dir, bool is_release, int point_x, int point_y)
{
      lv_obj_t *obj = NULL;
      lv_obj_t *scr = lv_scr_act();
      int width = lv_obj_get_width(scr);
      int height = lv_obj_get_height(scr);
     
      obj = get_cur_active_page(pull_dir);
      int obj_x = lv_obj_get_x(obj);
      int obj_y = lv_obj_get_y(obj);
      if(obj == NULL)
      {
         return;
      }
      
      if(obj == lv_obj_bot)
      {
         if(obj_y != 0 || abs(point_y - obj_y) < 30 || pull_dir == PULL_DIR_TOP)
           lv_obj_set_pos(obj, 0, point_y - height);
         lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);   

         if(is_release)
         {
            if(point_y < height / 2)
            {
               lv_obj_set_pos(obj, 0, -height);
               lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
            }
            else
            {
               lv_obj_set_pos(obj, 0, 0);
               lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);   
            }
         }
      }
      else if(obj == lv_obj_top)
      {
         if(obj_y != 0 || abs(point_y - obj_y) < 30 || pull_dir == PULL_DIR_BOTTOM)
           lv_obj_set_pos(obj, 0, point_y);
         lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);   
         

         if(is_release)
         {
            if(point_y < height / 2)
            {
               lv_obj_set_pos(obj, 0, 0);
               lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);                 
            }
            else
            {
               lv_obj_set_pos(obj, 0, height);
               lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
            }
         }
      }
     else if(obj == lv_obj_right)
     {
        if(obj_x != 0 || abs(point_x - obj_x) < 30 || pull_dir == PULL_DIR_LEFT)
           lv_obj_set_pos(obj, point_x - width, 0);
        lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);   
        

         if(is_release)
         {
            if(point_x < width / 2)
            {
               lv_obj_set_pos(obj, -width, 0);
               lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);                 
            }
            else
            {
               lv_obj_set_pos(obj, 0, 0);
               lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);
            }
         }        
     }
     else if(obj == lv_obj_left)
     {
        if(obj_x != 0 || abs(point_x - obj_x) < 30 || pull_dir == PULL_DIR_RIGHT)
           lv_obj_set_pos(obj, point_x, 0);
        lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);   
        
          if(is_release)
         {
            if(point_x < width / 2)
            {
               lv_obj_set_pos(obj, 0, 0);
               lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);                 
            }
            else
            {
               lv_obj_set_pos(obj, width, 0);
               lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
            }
         }          
     }
}